package com.work.keep.activity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ListView;

import com.work.keep.adapter.DemoAdapter;
import com.work.keep.base.BaseActivity;
import com.work.keep.ui.PullToRefreshBase;
import com.work.keep.ui.PullToRefreshRecyclerView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by dllo on 16/1/20.
 */
public class DemoActivity extends AppCompatActivity {
    private PullToRefreshRecyclerView pullToRefreshRecyclerView;
    private RecyclerView mRecycler;
    private DemoAdapter demoAdapter;
    private boolean mIsStart = true;
    private SimpleDateFormat mDateFormat = new SimpleDateFormat("MM-dd HH:mm");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pullToRefreshRecyclerView = new PullToRefreshRecyclerView(this);
        setContentView(pullToRefreshRecyclerView);
        pullToRefreshRecyclerView.setPullLoadEnabled(true);
        pullToRefreshRecyclerView.setScrollLoadEnabled(false);
        mRecycler = (RecyclerView) pullToRefreshRecyclerView.getRefreshableView();
        demoAdapter = new DemoAdapter(this);
        mRecycler.setLayoutManager(new LinearLayoutManager(this));
        mRecycler.setAdapter(demoAdapter);
        pullToRefreshRecyclerView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                mIsStart = true;
                new GetDataTask().execute();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                mIsStart = false;
                new GetDataTask().execute();
            }
        });
    }

    private class GetDataTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            // Simulates a background job.
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            //回来可以在这里做判断
            boolean hasMoreData = true;
            if (mIsStart) {
                demoAdapter.refresh();
            } else {
                demoAdapter.loadMore();
            }
            pullToRefreshRecyclerView.onPullDownRefreshComplete();
            pullToRefreshRecyclerView.onPullUpRefreshComplete();
            pullToRefreshRecyclerView.setHasMoreData(hasMoreData);
            String text = formatDateTime(System.currentTimeMillis());
            pullToRefreshRecyclerView.setLastUpdatedLabel(text);
            super.onPostExecute(aVoid);

        }
    }


    private String formatDateTime(long time) {
        if (0 == time) {
            return "";
        }

        return mDateFormat.format(new Date(time));
    }

}
